package com.ddwl.gate.service;

import org.springframework.util.CollectionUtils;
import cn.hutool.core.util.StrUtil;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.common.constant.MobileConfigEnum;
import com.ddwl.gate.dao.OperatorConfigDao;
import com.ddwl.gate.model.OperatorConfig;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.OperatorConfigBo;
import com.ddwl.schema.bo.gate.OperatorConfigPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.gate.OperatorConfigVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/9/23 20:30
 */
@Service
public class OperatorConfigService {

    @Resource
    private OperatorConfigDao operatorConfigDao;

    /**
     * 新增
     * @param bo
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(OperatorConfigBo bo) {
        OperatorConfig exist=getByNumSegment(bo.getNumSegment());
        ParamException.isNotNull(exist,"该号段已存在");
        OperatorConfig operatorConfig = BeanConverUtils.convertBean(bo, OperatorConfig.class);
        operatorConfig.setId(UUIDGenerator.getShortUUID());
        operatorConfigDao.insert(operatorConfig);
    }

    private OperatorConfig getByNumSegment(String numSegment) {
        OperatorConfig entity=new OperatorConfig();
        entity.setNumSegment(numSegment);
        return operatorConfigDao.getByCondition(entity);
    }

    /**
     * 分页
     * @param pageBo
     * @return
     */
    public PageVo<OperatorConfigVo> page(PageBo<OperatorConfigPageBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<OperatorConfig> pageInfo = new PageInfo<>(operatorConfigDao.getList(pageBo.getParam()));
        List<OperatorConfigVo> list = BeanConverUtils.convertBeanList(pageInfo.getList(), OperatorConfigVo.class);
        return new PageVo<>(pageInfo.getTotal(),pageBo.getPage(), pageBo.getPageSize(), list);
    }
    @Transactional(rollbackFor = Exception.class)
    public void update(OperatorConfigBo bo) {
        OperatorConfig exist=getByNumSegment(bo.getNumSegment());
        ParamException.isTrue((null != exist && !exist.getId().equals(bo.getId())), "该号段已存在");
        OperatorConfig entity=BeanConverUtils.convertBean(bo,OperatorConfig.class);
        operatorConfigDao.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        operatorConfigDao.deleteById(id);
    }

    public Byte getOperatorByMobile(String mobile) {
        if(StrUtil.isEmpty(mobile)||mobile.length()<3){
            return MobileConfigEnum.OperatorEnum.OTHER.getCode();
        }
        String numSegment="";
        OperatorConfig entity = new OperatorConfig();
        OperatorConfig oc=null;
        if (mobile.length() > 3) {
            numSegment = mobile.substring(0, 4);
            entity.setNumSegment(numSegment);
            oc= operatorConfigDao.selectOne(entity);
        }
        if(null==oc){
            numSegment = mobile.substring(0,3);
            entity.setNumSegment(numSegment);
            oc=operatorConfigDao.selectOne(entity);
        }
        if(null==oc||null==oc.getOperator()){
            return MobileConfigEnum.OperatorEnum.OTHER.getCode();
        }
        return oc.getOperator();
    }

    public void changeId() {
        List<OperatorConfig> list=operatorConfigDao.getAll();
        if (!CollectionUtils.isEmpty(list)) {
            Map<String, String> map = new HashMap<>();
            for (OperatorConfig oc : list) {
                String id = oc.getId();
                if (id.length() == 11) {
                    long longId = UUIDGenerator.reverseShortUUID(id);
                    map.put(id, String.valueOf(longId));
                }
            }
            if (!CollectionUtils.isEmpty(map)) {
                operatorConfigDao.changeId(map);
            }
        }
    }
}
